package com.wsm.springai.studydemo2.gh;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/query")
public class QueryController {

    @Autowired
    ChatClientBean chatClientBean;

    @CrossOrigin
    @GetMapping(value = "/getCustomer" ,produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> tool(@RequestParam(value = "message" , defaultValue = "你好") String message) {
        return chatClientBean.chatClient.prompt().user(message).stream().content();
    }


    // 将record移到类级别，并添加public修饰符
    public record Patient(int id, String name, String department, String status) {}

    @GetMapping("/queryList")
    public List<Patient> queryList() {
        // 返回持久化数据
        return GhInfoService.queryListInfo;
    }

    @GetMapping("/cancel")
    public String cancel(int id, String name) {
        ArrayList<Patient> queryListInfo = GhInfoService.queryListInfo;
        // 遍历查找并修改对应患者的status
        for (int i = 0; i < queryListInfo.size(); i++) {
            Patient patient = queryListInfo.get(i);
            if (patient.id() == id ) {
                // 创建新的Patient对象，状态更新为"已退费"
                Patient updatedPatient = new Patient(
                    patient.id(),
                    patient.name(),
                    patient.department(),
                    "已退费"
                );
                queryListInfo.set(i, updatedPatient);
                break;
            }
        }
        return "取消成功";
    }
}
